---------------------------------------------------------------
-- 英雄崛起（其实28天为一期的任务活动）
-- 与创角时间相关

-- tip 32
-- db.heroGrowUp.groupID 		活动组id
-- db.heroGrowUp.time 			活动开始的时间戳
-- db.heroGrowUp.lv 			等级
-- db.heroGrowUp.lvGet 			等级领取
-- db.heroGrowUp.lvExGet 		等级额外领取
-- db.heroGrowUp.jifen 			当前积分
-- db.heroGrowUp.isBuy 			是否购买

-- db.heroGrowUp.task[mainType] = taskList
-- taskList.time  				刷新时间
-- taskList.records  			任务进度 [id] = value
-- taskList.gets 				领取记录 [id] = true
---------------------------------------------------------------

local PresentExcel = require("excel.present")
local OpenActExcel = require("excel.openAct")
local Util = require("common.Util")
local Lang = require("common.Lang")
local Msg = require("core.Msg")
local Broadcast = require("broadcast.Broadcast")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local CombatDefine = require("combat.CombatDefine")
local BuyLogic = require("topup.BuyLogic")
local HeroDefine = require("hero.HeroDefine")
local RoleDBLogic = require("role.RoleDBLogic")
local YunYingLogic = require("yunying.YunYingLogic")
local SceneHandler = require("scene.Handler")
local CommonDB = require("common.CommonDB")
local ItemExcel = require("excel.item")
local ObjHuman = require("core.ObjHuman")
local Config = require("Config")
local AbsActExcel = require("excel.absAct")
local AbsActLogic = require("absAct.AbsActLogic")

DAY_TIME_SEC = 1 * 86400
WEEK_TIME_SEC = 7 * 86400
ACT_TIME_SEC1 = 28 * 86400
ACT_TIME_SEC2 = 14 * 86400

-- 任务分类
MAINTYPE_DAY = 1 		-- 每日
MAINTYPE_WEEK = 2  		-- 每周
MAINTYPE_MONTH = 3 		-- 每期
MAINTYPE_CNT = 3 

-- 任务状态
TASK_STATE_NONE = 0 	-- 不可领
TASK_STATE_CAN = 1 		-- 可领
TASK_STATE_HAD = 2 		-- 已领

-- 任务类型
TASKTYPE1 = 1 	-- o登录游戏
TASKTYPE2 = 2 	-- o天命召唤
TASKTYPE3 = 3 	-- o占卜合成
TASKTYPE4 = 4 	-- o消耗x金币
TASKTYPE5 = 5 	-- o消耗x钻石
TASKTYPE6 = 6 	-- o战报点赞X次
TASKTYPE7 = 7 	-- o皇冠联赛战场胜利X次
TASKTYPE8 = 8 	-- o合成X次任意符文
TASKTYPE9 = 9 	-- o冰龙巢穴
TASKTYPE10 = 10 -- o随机商店购买道具
TASKTYPE11 = 11 -- o上阵指定英雄XX，皇冠联赛战场获得X场胜利
TASKTYPE12 = 12 -- o获取X个Y星英雄
TASKTYPE13 = 13 -- o挑战战神殿
TASKTYPE14 = 14 -- o王者争霸32强
TASKTYPE15 = 15 -- o完成X次悬赏任务
TASKTYPE16 = 16 -- o圣树试炼胜利X次
TASKTYPE17 = 17 -- o接取X次红色悬赏任务
TASKTYPE18 = 18 -- o消耗X英雄经验
TASKTYPE19 = 19 -- o洗练X次任意装备
TASKTYPE20 = 20 -- o遗迹探宝，击败x个守卫
TASKTYPE21 = 21 -- o分解装备

DG_ABS_ACT_ID = 8001

HGU_AFTER_START = true


function initAfterStart()
    HGU_AFTER_START = false
end

function initAfterHot()
    if HGU_AFTER_START then
        return
    end
    local workManStartTime,workManEndTime,groupID = getActStartTime()
    -- 活动未开
    if not workManStartTime then
        return
    end

    --重置任务
    TASKTYPE_2_MAINTYPES = {}
    MAINTYPE_2_IDLIST = {}
    -- 设置新任务
    for id, cf in pairs(AbsActExcel.heroGrowUpTask) do

		if not TASKTYPE_2_MAINTYPES[cf.taskType] then
			TASKTYPE_2_MAINTYPES[cf.taskType] = {}
		end

        if not MAINTYPE_2_IDLIST[cf.mainType] then
			MAINTYPE_2_IDLIST[cf.mainType] = {}
		end

		local len = #MAINTYPE_2_IDLIST[cf.mainType]
		MAINTYPE_2_IDLIST[cf.mainType][len + 1] = id

		local list = TASKTYPE_2_MAINTYPES[cf.taskType]
		local value = cf.taskArgs[1]
		local key = cf.taskArgs[2]

        if type(cf.taskArgs[2]) == "table" then
            local lenth = #cf.taskArgs[2]
            local randomPos = math.random(1,lenth)
            key = cf.taskArgs[2][randomPos]
        end

		if key then
			list[cf.mainType] = list[cf.mainType] or {}
			list[cf.mainType][key] = math.max(list[cf.mainType][key] or 0, value)
		else
			list[cf.mainType] = math.max(list[cf.mainType] or 0, value)
		end
	end
    
	GROUPID_2_LVCONFIGS = {}
	for _, cf in ipairs(AbsActExcel.heroGrowUpLv) do
		if not GROUPID_2_LVCONFIGS[cf.groupID] then
			GROUPID_2_LVCONFIGS[cf.groupID] = {}
		end
		GROUPID_2_LVCONFIGS[cf.groupID][cf.lv] = cf
	end
end

-- 本期活动开始时间
function getActStartTime()
    --local id = AbsActLogic.getIDByActID(human, DG_ABS_ACT_ID)

    local absActConfig = AbsActExcel.absActivity[DG_ABS_ACT_ID]
    if absActConfig.actId and absActConfig.actId ~= 0 then
        return absActConfig.realStartTime,absActConfig.realEndTime,absActConfig.actId
    end
end

-- 活动剩余时间
function getActLeftTime()
    local absActConfig = AbsActExcel.absActivity[DG_ABS_ACT_ID]
    if absActConfig.actId then
        return absActConfig.realEndTime - os.time()
    end
    return 0
end

-- 任务周期
function getTaskTime(mainType)
	if mainType == MAINTYPE_DAY then
		return DAY_TIME_SEC
	elseif mainType == MAINTYPE_WEEK then
		return WEEK_TIME_SEC
	elseif mainType == MAINTYPE_MONTH then
		return ACT_TIME_SEC1
	end	
end

-- 本期任务开始时间
function getTaskStartTime(actStartTime, mainType)
	local time = getTaskTime(mainType)
	if not time then return end

	local sec = os.time() - actStartTime
	local cnt = math.floor(sec / time)
	return actStartTime + cnt * time
end

-- 获取任务剩余时间
function getTaskLeftTime(actStartTime, mainType)
	local time = getTaskTime(mainType)
	if not time then return end
	local actLeftTime = getActLeftTime(actStartTime)
	return actLeftTime % time
end

-- 是否开放
function isOpen(human)
    local openDay = CommonDB.getServerOpenDay()
    local absActConfig = AbsActExcel.absActivity[DG_ABS_ACT_ID]
    if openDay and openDay < absActConfig.openDay then
        return
    end
	if not getActStartTime(human) then
		return
	end
	return true
end

-- 是否红点
function isRed(human)
	return isTaskRed(human) or isRewardRed(human)
end

-- 积分任务是否有红点
function isTaskRed(human)
	for mainType = 1, MAINTYPE_CNT do
		if isTaskRedByType(human, mainType) then
			return true
		end
	end
end

-- 有奖励可领取
function isRewardRed(human)
	local heroGrowUpDB = human.db.heroGrowUp
	if not heroGrowUpDB then 
        return 
    end

    local actStartTime,actEndTime,groupID = getActStartTime()
	local lvConfigs = getLvConfigs(groupID)
	if not actStartTime then return end		
    if not lvConfigs then
        return
    end

    local maxLv = #lvConfigs
    for i = 1,heroGrowUpDB.lv do
        if i <= maxLv then
            if getGetReward(human,i) == 1 or not getGetExReward(human,i) == 1 then
                return true
            end
        else
            local lvDefine = lvConfigs[1]
            local flag = (i - maxLv) % lvDefine.lastNeedLv
            if flag == 0 then
                if getGetReward(human,i) == 1 or getGetExReward(human,i) == 1 then
                    return true
                end
            end
        end
    end
    return
end
-- 根据分类判断是否有红点
function isTaskRedByType(human, mainType)
	local list = getTaskIDList(mainType)
	local listLen = list and #list or 0
	for i = 1, listLen do
		local id = list[i]
		local cf = AbsActExcel.heroGrowUpTask[id]
		if getTaskState(human, id, cf) == TASK_STATE_CAN then
			return true
		end
	end
end

-- 根据类型获取任务id
function getTaskIDList(mainType)
	return TASKTYPE_2_MAINTYPES and MAINTYPE_2_IDLIST[mainType]
end

-- 根据任务类型获取影响的分类
-- [taskType] = {[mainType1] = maxValue, [mainType2][key] = maxValue2}
function getMainTypesByTaskType(taskType)
	return TASKTYPE_2_MAINTYPES and TASKTYPE_2_MAINTYPES[taskType]
end

-- 最多x期
local MAX_GROUPID = nil
function getMaxGroupID()
	if not MAX_GROUPID then
		MAX_GROUPID = 0
		for _, cf in ipairs(AbsActExcel.heroGrowUpLv) do
			if cf.groupID > MAX_GROUPID then
				MAX_GROUPID = cf.groupID
			end
		end
	end
	return MAX_GROUPID
end

-- 根据第x期获取配置
function getLvConfigs(groupID)
	return (groupID and GROUPID_2_LVCONFIGS) and GROUPID_2_LVCONFIGS[groupID]
end

-------------------------------------------- db -------------------------------------------------------
-- 初始DB
function initDB(human, actStartTime)
    local _,_,groupID = getActStartTime()
	local lvConfigs = getLvConfigs(groupID)
    
    if not lvConfigs then
        return
    end

	local heroGrowUpDB = {}
	heroGrowUpDB.time = actStartTime
    heroGrowUpDB.groupID = groupID
	heroGrowUpDB.lv = 0
    heroGrowUpDB.lvGet = {}		
    heroGrowUpDB.lvExGet = {}
    heroGrowUpDB.jifen = 0 
	heroGrowUpDB.isBuy = nil
	heroGrowUpDB.task = {}
	human.db.heroGrowUp = heroGrowUpDB
end

-- 初始任务DB
function initTaskDB(human, actStartTime, mainType)
	local heroGrowUpDB = human.db.heroGrowUp
	local taskStartTime = getTaskStartTime(actStartTime, mainType)
	local taskList = {}
	taskList.time = taskStartTime
	taskList.records = {}
	taskList.gets = {}
	heroGrowUpDB.task[mainType] = taskList
end

-- 检查DB是否需要重置
function checkDB(human, actStartTime,groupID)
	local heroGrowUpDB = human.db.heroGrowUp
	if not heroGrowUpDB then 
        return 
    end

    -- 新一期活动
	if heroGrowUpDB.groupID ~= groupID then
        heroGrowUpDB.groupID = groupID
	    heroGrowUpDB.time = actStartTime
	    heroGrowUpDB.lv = 0 
        heroGrowUpDB.lvGet = {}		
        heroGrowUpDB.lvExGet = {}
	    heroGrowUpDB.jifen = 0 
	    heroGrowUpDB.isBuy = heroGrowUpDB.isBuy and 0 or nil
	    heroGrowUpDB.task = {}
		return
	end

	-- 任务记录重置
	for mainType, taskList in pairs(heroGrowUpDB.task) do
		local taskStartTime = getTaskStartTime(actStartTime, mainType)
		if taskList.time ~= taskStartTime then
			heroGrowUpDB.task[mainType] = nil
		end
	end
end

-- 获取等级
function getLv(human)
	local heroGrowUpDB = human.db.heroGrowUp
	if not heroGrowUpDB then 
		return 0
	end
	return heroGrowUpDB.lv or 0	
end

-- 获取当前积分
function getJifen(human)
	local heroGrowUpDB = human.db.heroGrowUp
	if not heroGrowUpDB then 
		return 0
	end
	return heroGrowUpDB.jifen or 0
end

-- 增加积分
function addJifen(human, value)
	local actStartTime,actEndTime,groupID = getActStartTime()
	local lvConfigs = getLvConfigs(groupID)
	if not actStartTime then return end		
    
    if not lvConfigs then
        return
    end
	local lvdefine = lvConfigs[1]
	checkDB(human, actStartTime,groupID)
	if not human.db.heroGrowUp then
		initDB(human, actStartTime)
	end
	local heroGrowUpDB = human.db.heroGrowUp	
	heroGrowUpDB.jifen = (heroGrowUpDB.jifen or 0) + value
	local addLv = math.floor(heroGrowUpDB.jifen / lvdefine.needJifen)
	local oldLv = heroGrowUpDB.lv or 0
	local newLv = oldLv + addLv
	heroGrowUpDB.jifen = heroGrowUpDB.jifen - addLv * lvdefine.needJifen
	heroGrowUpDB.lv = newLv
    if oldLv ~= newLv then
        YunYingLogic.updateIcon(YYInfo[DG_ABS_ACT_ID], human)
    end
end

-- 是否购买王者礼包
function isBuyKing(human)
	local heroGrowUpDB = human.db.heroGrowUp
	if not heroGrowUpDB then 
		return 
	end
	return heroGrowUpDB.isBuy
end

-- 设置购买
function setBuyKing(human, actStartTime)
	if not human.db.heroGrowUp then
		initDB(human, actStartTime)
	end
	local heroGrowUpDB = human.db.heroGrowUp
	heroGrowUpDB.isBuy = 1
end

-- 设置领奖
function setGetReward(human,lv)
	if not human.db.heroGrowUp then
		initDB(human, actStartTime)
	end
	local heroGrowUpDB = human.db.heroGrowUp
	heroGrowUpDB.lvGet = heroGrowUpDB.lvGet or {}
    heroGrowUpDB.lvGet[lv] = 2
end

function getGetReward(human,lv)
	local heroGrowUpDB = human.db.heroGrowUp
	if not heroGrowUpDB then 
		return 0
	end

    if lv > heroGrowUpDB.lv then
        return 0
    end

    if not heroGrowUpDB.lvGet or not heroGrowUpDB.lvGet[lv] then
        return 1
    else
        return heroGrowUpDB.lvGet[lv]
    end
end

function getGetExReward(human,lv)
	local heroGrowUpDB = human.db.heroGrowUp
	if not heroGrowUpDB then 
		return 0
	end
    
    if lv > heroGrowUpDB.lv then
        return 0
    end

    if not heroGrowUpDB.lvExGet or not heroGrowUpDB.lvExGet[lv] then
        return 1
    else
        return heroGrowUpDB.lvExGet[lv]
    end
end

-- 设置领奖
function setGetExReward(human,lv)
	if not human.db.heroGrowUp then
		initDB(human, actStartTime)
	end
	local heroGrowUpDB = human.db.heroGrowUp
	heroGrowUpDB.lvExGet = heroGrowUpDB.lvExGet or {}
    heroGrowUpDB.lvExGet[lv] = 2
end


-- 是否领取
function isGetTask(human, id, cf)
	local heroGrowUpDB = human.db.heroGrowUp
	if not heroGrowUpDB then 
		return 
	end
	local taskList = heroGrowUpDB.task[cf.mainType]
	if not taskList then return end
	if taskList.gets[id] then
		return true
	end
end

-- 设置已领取
function setGetTask(human, actStartTime, id, cf)
	if not human.db.heroGrowUp then
		initDB(human, actStartTime)
	end
	local heroGrowUpDB = human.db.heroGrowUp	
	if not heroGrowUpDB.task[cf.mainType] then
		initTaskDB(human, actStartTime, cf.mainType)
	end	
	heroGrowUpDB.task[cf.mainType].gets[id] = 1
end

local function getValueByKey(human, mainType, taskType, key)
	if taskType == TASKTYPE1 then
		return 1
	end
	local heroGrowUpDB = human.db.heroGrowUp
	local taskList = heroGrowUpDB and heroGrowUpDB.task[mainType]
	if not taskList then 
		return 0
	end
	local record = taskList.records[taskType]
	if not key then
		return record or 0
	end
	return record and record[key] or 0
end

-- 当前进度
function getValue(human, cf)
	local key = cf.taskArgs[2]
    if type(key) == "table" then
        local list = getMainTypesByTaskType(cf.taskType)
        for k,v in pairs(list[cf.mainType]) do
            key = k
            break
        end
    end
	return getValueByKey(human, cf.mainType, cf.taskType, key)
end

-- 更新进度
function updateValue(human, actStartTime, mainType, taskType, key, value)
	if not human.db.heroGrowUp then
		initDB(human, actStartTime)
	end
	local heroGrowUpDB = human.db.heroGrowUp	
	if not heroGrowUpDB.task[mainType] then
		initTaskDB(human, actStartTime, mainType)
	end
	local taskList = heroGrowUpDB.task[mainType]
	if key then
		taskList.records[taskType] = taskList.records[taskType] or {}
		taskList.records[taskType][key] = value
	else
		taskList.records[taskType] = value
	end	
end

-- 最大进度
function getMaxValue(cf)
	return cf.taskArgs[1]
end

-- 获取任务状态
function getTaskState(human, id, cf)
	if isGetTask(human, id, cf) then
		return TASK_STATE_HAD
	end

	local value = getValue(human, cf)
	local maxValue = getMaxValue(cf)
	if value >= maxValue then
		return TASK_STATE_CAN
	end
	return TASK_STATE_NONE
end

-- 获取任务名字
function getTaskName(cf)
	if cf.taskType == TASKTYPE11 then
        local list = TASKTYPE_2_MAINTYPES[TASKTYPE11][cf.mainType]
		local heroID = cf.taskArgs[2]
        for k,v in pairs(list) do
            heroID = k
            break
        end
		local heroConfig = HeroDefine.getConfig(heroID)
		return Util.format(cf.name, heroConfig.name)
	end
	return cf.name
end

-- 获取任务描述
function getTaskDesc(cf)
	if cf.taskType == TASKTYPE18 or 
        cf.taskType == TASKTYPE4 or
        cf.taskType == TASKTYPE12 then
		return Util.format(cf.desc, cf.taskArgs[1], cf.taskArgs[2])
	elseif cf.taskType == TASKTYPE14 then
		return Util.format(cf.desc, cf.taskArgs[2])
	end
	return Util.format(cf.desc, cf.taskArgs[1])
end

-------------------------------------------- msg -------------------------------------------------------
-- 封装基础信息
function fontBaseNet(net, human, actStartTime, lvdefine)
	net.leftTime = getActLeftTime(actStartTime)
	net.lv = getLv(human)
	net.jifen = getJifen(human)
	net.maxJifen = lvdefine.needJifen
    local isBuy = isBuyKing(human)
	net.isBuyKing = isBuy == 1 and 1 or 0
	net.hasTaskRed = isRed(human) and 1 or 0	
end

-- 封装任务信息
function fontTaskNet(net, id, human)
	local cf = AbsActExcel.heroGrowUpTask[id]
	net.id = id 
	net.taskName = getTaskName(cf)
	net.taskDesc = getTaskDesc(cf)
	net.maxValue = getMaxValue(cf)
	net.nowValue = math.min(getValue(human, cf), net.maxValue)	
	Grid.makeItem(net.item, cf.items[1][1], cf.items[1][2])
	net.panelID = cf.panelID
	net.state = getTaskState(human, id, cf)
end

-- 积分任务界面
function sendTaskQuery(human, mainType)
	local actStartTime,actEndTime, groupID = getActStartTime()
	local lvConfigs = getLvConfigs(groupID)
	if not actStartTime then
		return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
	end
    
    if not lvConfigs then
        return
    end
	local leftTime = getTaskLeftTime(actStartTime, mainType)
	if not leftTime then return end

	local lvdefine = lvConfigs[1]
	checkDB(human, actStartTime,groupID)
	local list = getTaskIDList(mainType)
	local msgRet = Msg.gc.GC_HERO_GROWUP_TASK_QUERY
    msgRet.endTime = actEndTime
    msgRet.startTime = actStartTime
    msgRet.leftTime = actEndTime - os.time()
	fontBaseNet(msgRet.data, human, actStartTime, lvdefine)
    msgRet.data.hasTaskRed = isTaskRed(human) and 1 or 0
	msgRet.mainType = mainType
	msgRet.taskLeftTime = leftTime
	msgRet.taskList[0] = list and #list or 0
	for i = 1, msgRet.taskList[0] do
		local id = list[i]
		fontTaskNet(msgRet.taskList[i], id, human)
	end
	msgRet.taskReds[0] = MAINTYPE_CNT
	for i = 1, msgRet.taskReds[0] do
		msgRet.taskReds[i] = isTaskRedByType(human, i) and 1 or 0
	end
	--Msg.trace(msgRet)
	Msg.send(msgRet, human.fd)
end

-- 领取任务奖励
function getTaskItems(human, id)
	local actStartTime,actEndTime,groupID = getActStartTime()
	if not actStartTime then
		return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
	end
	local cf = AbsActExcel.heroGrowUpTask[id]
	if not cf then return end

	checkDB(human, actStartTime,groupID)
	local state = getTaskState(human, id, cf)
	if state == TASK_STATE_NONE then
		return Broadcast.sendErr(human, Lang.YUNYING_GET_ERR_CONDITION)
	end
	if state == TASK_STATE_HAD then
		return Broadcast.sendErr(human, Lang.YUNYING_GET_ERR_HADGET)
	end

	setGetTask(human, actStartTime, id, cf)
	for _, item in ipairs(cf.items) do
		local itemID = item[1]
		local itemCnt = item[2]
		BagLogic.addItem(human, itemID, itemCnt, "hero_growup_task")
	end
	BagLogic.sendItemGetList1(human, cf.items)

	sendTaskQuery(human, cf.mainType)
	YunYingLogic.updateIcon(YYInfo[DG_ABS_ACT_ID], human)
end

-- 查看王者礼包信息
function sendKingQuery(human)
	if not SceneHandler.canCharge(human) then
        return
    end
	local actStartTime,actEndTime, groupID = getActStartTime()
	local lvConfigs = getLvConfigs(groupID)
	if not actStartTime then
		return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
	end	
    
    if not lvConfigs then
        return
    end
	local lvdefine = lvConfigs[1].kingBuyItems	
	checkDB(human, actStartTime,groupID)
    
    local isBuy = isBuyKing(human)
    if isBuy then
        lvdefine = lvConfigs[1].kingBuyItems2	
    end

	local msgRet = Msg.gc.GC_HERO_GROWUP_KING_QUERY
	msgRet.items[0] = math.min(#lvdefine, #msgRet.items)
	for i = 1, msgRet.items[0] do
		local itemID = lvdefine[i][1]
		local itemCnt = lvdefine[i][2]		
		Grid.makeItem(msgRet.items[i], itemID, itemCnt)
	end
	BuyLogic.fontBuyItem(human, msgRet.buyItem, lvConfigs[1].buyID)
	msgRet.isBuyKing = isBuy == 1 and 1 or 0
	--Msg.trace(msgRet)
	Msg.send(msgRet, human.fd)
end

-- 奖励界面查看
function sendRewardQuery(human)
	local actStartTime,actEndTime, groupID = getActStartTime()
	local lvConfigs = getLvConfigs(groupID)
	if not actStartTime or not lvConfigs then
		return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
	end

	local lvdefine = lvConfigs[1]
	checkDB(human, actStartTime,groupID)
	local msgRet = Msg.gc.GC_HERO_GROWUP_REWARD_QUERY
	fontBaseNet(msgRet.data, human, actStartTime, lvdefine)
    msgRet.data.hasTaskRed = isRewardRed(human) and 1 or 0
	msgRet.rewardList[0] = #lvConfigs
    
    local nowLv = getLv(human)

	for lv = 1, msgRet.rewardList[0] do
		local net = msgRet.rewardList[lv]
		local cf = lvConfigs[lv]
		net.lv = lv 
		net.items[0] = #cf.items
        net.state = 0
        net.stateEx = 0
		for i = 1, net.items[0] do
			local itemID = cf.items[i][1]
			local itemCnt = cf.items[i][2]
			Grid.makeItem(net.items[i], itemID, itemCnt)
		end
        if lv <= nowLv then
            net.state = getGetReward(human,lv)
        end
		net.itemsKing[0] = #cf.itemsKing
		for i = 1, net.itemsKing[0] do
			local itemID = cf.itemsKing[i][1]
			local itemCnt = cf.itemsKing[i][2]
			Grid.makeItem(net.itemsKing[i], itemID, itemCnt)
		end		
        if lv <= nowLv then
            net.stateEx = getGetExReward(human,lv)
        end
	end
    local maxLv = #lvConfigs
	local exRewardNet = msgRet.exReward
    local nextLv = maxLv + lvdefine.lastNeedLv
	
	exRewardNet.needLv = lvdefine.lastNeedLv
	exRewardNet.items[0] = #lvdefine.lastItems
    exRewardNet.state = 0
    exRewardNet.stateEx = 0

    while true do
        if nextLv > nowLv then
            break
        end
        local nextState = getGetReward(human,nextLv)
        if not nextState or nextState == 1 then
            break
        end
        nextLv = nextLv + lvdefine.lastNeedLv
    end

    exRewardNet.lv = nextLv

    exRewardNet.state = getGetReward(human,nextLv)

    nextLv = maxLv + lvdefine.lastNeedLv
    while true do
        if nextLv > nowLv then
            break
        end
        local nextState = getGetExReward(human,nextLv)
        if not nextState or nextState == 1 then
            break
        end
        nextLv = nextLv + lvdefine.lastNeedLv
    end
    exRewardNet.exLv = nextLv

    exRewardNet.stateEx = getGetExReward(human,nextLv)
    local isBuy = isBuyKing(human)
    if isBuy ~= 1 then
        exRewardNet.stateEx = 0
    end
	for i = 1, exRewardNet.items[0] do
		local itemID = lvdefine.lastItems[i][1]
		local itemCnt = lvdefine.lastItems[i][2]
		Grid.makeItem(exRewardNet.items[i], itemID, itemCnt)
	end
	exRewardNet.itemsKing[0] = #lvdefine.lastItemsKing
	for i = 1, exRewardNet.itemsKing[0] do
		local itemID = lvdefine.lastItemsKing[i][1]
		local itemCnt = lvdefine.lastItemsKing[i][2]
		Grid.makeItem(exRewardNet.itemsKing[i], itemID, itemCnt)
	end	
	--Msg.trace(msgRet)
	Msg.send(msgRet, human.fd)
end

-------------------------------------------- 回调 -------------------------------------------------------

-- 升级回调
function getLvReward(human, lv)
	local actStartTime,actEndTime, groupID = getActStartTime()
    if not groupID then
        return
    end
    local nowLv = getLv(human)
    if lv > nowLv then
        return
    end

    local normalGet = getGetReward(human,lv)
    local exGet = getGetExReward(human,lv)
	
	local lvConfigs = getLvConfigs(groupID)
    if not lvConfigs then
        return
    end
	local lvdefine = lvConfigs[1]
	local list = {}
	local maxLv = #lvConfigs
	local isBuy = isBuyKing(human)
	local cf = lvConfigs[lv]
	local items = cf and cf.items 
	local itemsKing = cf and cf.itemsKing
	if (not cf) and (lv - maxLv) % lvdefine.lastNeedLv == 0 then
		items = lvdefine.lastItems
		itemsKing = lvdefine.lastItemsKing		
	end
	if items and normalGet ~= 2 then
		for _, item in ipairs(items) do
			local itemID = item[1]
			local itemCnt = item[2]
			list[itemID] = (list[itemID] or 0) + itemCnt
		end
	end
	if itemsKing and isBuy == 1 and exGet ~= 2 then
		for _, item in ipairs(itemsKing) do
			local itemID = item[1]
			local itemCnt = item[2]
			list[itemID] = (list[itemID] or 0) + itemCnt
		end			
	end
	if not next(list) then return end
    
    setGetReward(human,lv)
    if isBuy == 1 then
        setGetExReward(human,lv)
    end

    
	YunYingLogic.updateIcon(YYInfo[DG_ABS_ACT_ID], human)

	local msgRet = Msg.gc.GC_HERO_GROWUP_LVUP
	msgRet.lv = nowLv
	msgRet.items[0] = 0
	for itemID, itemCnt in pairs(list) do
		BagLogic.addItem(human, itemID, itemCnt, "hero_growup")
		if msgRet.items[0] < #msgRet.items then
			msgRet.items[0] = msgRet.items[0] + 1
			Grid.makeItem(msgRet.items[msgRet.items[0]], itemID, itemCnt)
		end
	end
	BagLogic.sendItemGetList2(human, list)
	msgRet.isBuyKing = isBuy == 1 and 1 or 0
	--Msg.trace(msgRet)
	Msg.send(msgRet, human.fd)

end

-- 购买王者礼包ii
function buyKing(human)
	local actStartTime,actEndTime, groupID = getActStartTime()
	local lvConfigs = getLvConfigs(groupID)
	if not actStartTime then return end	
    
    if not lvConfigs then
        return
    end
	local lvdefine = lvConfigs[1]	
	checkDB(human, actStartTime,groupID)
    local isBuy = isBuyKing(human)
	if isBuy == 1 then return end
	setBuyKing(human, actStartTime)	

	-- 计算可以给的东西
--	local maxLv = #lvConfigs
--	local nowLv = getLv(human)
--	local list = {}
--	for lv = 1, nowLv do
--		local cf = lvConfigs[lv]
--		local itemsKing = cf and cf.itemsKing
--		if (not cf) and (lv - maxLv) % lvdefine.lastNeedLv then
--			itemsKing = lvdefine.lastItemsKing		
--		end
--		if itemsKing then
--			for _, item in ipairs(itemsKing) do
--				local itemID = item[1]
--				local itemCnt = item[2]
--				list[itemID] = (list[itemID] or 0) + itemCnt
--			end			
--		end
--	end
    local list = {}
    local config = lvdefine.kingBuyItems
    if isBuy then
        config = lvdefine.kingBuyItems2
    end
	-- 购买王者后直接赠送的东西
	for _, item in ipairs(config) do
		local itemID = item[1]
		local itemCnt = item[2]		
		list[itemID] = (list[itemID] or 0) + itemCnt
	end

	-- 给东西
	for itemID, itemCnt in pairs(list) do
		BagLogic.addItem(human, itemID, itemCnt, "hero_growup")
	end	
	BagLogic.sendItemGetList2(human, list)

    YunYingLogic.updateIcon(YYInfo[DG_ABS_ACT_ID], human)
	local msgRet = Msg.gc.GC_HERO_GROWUP_BUY_OK
	fontBaseNet(msgRet.data, human, actStartTime, lvdefine)
	Msg.send(msgRet, human.fd)
    sendRewardQuery(human)
end

-- 子回调
local function doCallback(human, actStartTime, mainType, taskType, maxValue, key, value)
	local oldValue = getValueByKey(human, mainType, taskType, key)
	if oldValue >= maxValue then
		return
	end

	local newValue = oldValue
    -- 任务类型11 指定英雄 竞技场胜利指定次数
	if taskType == TASKTYPE11 then
		local isFind = nil
		for _, obj in pairs(value.objList) do
			if obj.side == CombatDefine.ATTACK_SIDE and
				obj.id == key then
				isFind = true
				break
			end
		end
		if not isFind then return end
		newValue = oldValue + 1
	elseif taskType == TASKTYPE12 then
		if key ~= value then
			return
		end
		newValue = oldValue + 1
	elseif taskType == TASKTYPE14 then
		if value > key then
			return
		end
		newValue = oldValue + 1
	else
		newValue = math.min(oldValue + value, maxValue)
	end	
	updateValue(human, actStartTime, mainType, taskType, key, newValue)
	return true
end

-- 任务回调
function onCallback(human, taskType, value)
	local actStartTime,actEndTime,groupID = getActStartTime()
	if not actStartTime then return end	

	local mainTypes = getMainTypesByTaskType(taskType)
	if not mainTypes then return end

	checkDB(human, actStartTime,groupID)
	local isChange = nil
	for mainType, mainValue in pairs(mainTypes) do
		local mainValueType = type(mainValue)
		if mainValueType == "number" then
			if doCallback(human, actStartTime, mainType, taskType, mainValue, nil, value) then
				isChange = true
			end
		elseif mainValueType == "table" then
			for key, maxValue in pairs(mainValue) do
				if doCallback(human, actStartTime, mainType, taskType, maxValue, key, value) then
					isChange = true
				end
			end
		end		
	end
	if isChange then
		YunYingLogic.updateIcon(YYInfo[DG_ABS_ACT_ID], human)
		Msg.send(Msg.gc.GC_HERO_GROWUP_TASK_REFRESH, human.fd)
	end
	return isChange
end

-- 任务回调 uuid
local FakeHuman = {}
function onCallbackByUuid(uuid, taskType, value)
	local db, online = RoleDBLogic.getDb(uuid)
	if not db then return end

	FakeHuman.db = db
	if onCallback(FakeHuman, taskType, value) and not online then
		RoleDBLogic.saveRole(db)
	end
end

function buyJifen(human,itemID,cnt)
    local actStartTime,actEndTime,groupID = getActStartTime()
	if not actStartTime then return end	

    local now = os.time()
    if actEndTime - now > 3*86400 then
        return
    end

    checkDB(human, actStartTime,groupID)

    local config = ItemExcel.commonBuy[itemID]
    if not config then
        return
    end 

    local price = config.price[2]*cnt
    if not ObjHuman.checkRMB(human,price) then
        return
    end

    ObjHuman.decZuanshi(human, - price, "hero_growup")

    addJifen(human, 1000*cnt)

    local msgRet = Msg.gc.GC_HERO_GROWUP_BUY_JIFEN
    msgRet.lv = human.db.heroGrowUp.lv or 0
    msgRet.jifen = human.db.heroGrowUp.jifen or 0
    msgRet.maxJifen = AbsActExcel.heroGrowUpLv[1].needJifen
    Msg.send(msgRet,human.fd)
end


-- 活动循环，开启与结束
function actLoop()
    local workManStartTime,workManEndTime,groupID = getActStartTime()
    local workMan = CommonDB.getWorkMan()
    -- 活动未开，且commonDB活动开，则关闭活动
    if not workManStartTime and workMan.state then
        CommonDB.setWorkManState()
        return
    end
    
    -- 活动开，且commonDB活动未开，则开启活动
    -- 活动id不符
    if (workManStartTime and not workMan.state) or groupID ~=  workMan.groupID then
        CommonDB.setWorkManState(true)
        CommonDB.setWorkManGroup(groupID)
        CommonDB.setWorkManTs(workManStartTime)
        --重置任务
        TASKTYPE_2_MAINTYPES = {}
        MAINTYPE_2_IDLIST = {}
        -- 设置新任务
        for id, cf in pairs(AbsActExcel.heroGrowUpTask) do

			if not TASKTYPE_2_MAINTYPES[cf.taskType] then
				TASKTYPE_2_MAINTYPES[cf.taskType] = {}
			end

            if not MAINTYPE_2_IDLIST[cf.mainType] then
				MAINTYPE_2_IDLIST[cf.mainType] = {}
			end

			local len = #MAINTYPE_2_IDLIST[cf.mainType]
			MAINTYPE_2_IDLIST[cf.mainType][len + 1] = id

			local list = TASKTYPE_2_MAINTYPES[cf.taskType]
			local value = cf.taskArgs[1]
			local key = cf.taskArgs[2]

            if type(cf.taskArgs[2]) == "table" then
                local lenth = #cf.taskArgs[2]
                local randomPos = math.random(1,lenth)
                key = cf.taskArgs[2][randomPos]
            end

			if key then
				list[cf.mainType] = list[cf.mainType] or {}
				list[cf.mainType][key] = math.max(list[cf.mainType][key] or 0, value)
			else
				list[cf.mainType] = math.max(list[cf.mainType] or 0, value)
			end
		end
        
	    GROUPID_2_LVCONFIGS = {}
	    for _, cf in ipairs(AbsActExcel.heroGrowUpLv) do
	    	if not GROUPID_2_LVCONFIGS[cf.groupID] then
	    		GROUPID_2_LVCONFIGS[cf.groupID] = {}
	    	end
	    	GROUPID_2_LVCONFIGS[cf.groupID][cf.lv] = cf
	    end
        return
    end
end
